﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IndoReputation.Models.BusinessLogic;
using Telerik.Web.Mvc;
using IndoReputation.Views.ViewModels;
using IndoReputation.Models.BusinessObject;
using IndoReputation.Models.BusinessObject.Collections;

namespace IndoReputation.Controllers
{
    [Authorize]
    public class CityController : BaseController
    {
        //
        // GET: /City/

        public ActionResult Index()
        {
            ViewData["ajax"] = true;
            ViewData["scrolling"] = true;
            ViewData["paging"] = true;
            ViewData["filtering"] = true;
            ViewData["grouping"] = true;
            ViewData["sorting"] = true;
            ViewData["showFooter"] = true;
            return View(CityBOL.GetAllCityJoin());
        }

        [GridAction]
        public ActionResult _Index()
        {
            return View(new GridModel(CityBOL.GetAllCityJoin()));
        }

        [HttpGet]
        public ActionResult Add()
        {
            var country = CountryBOL.GetAllCountry();           
            var model = new CityViewModel
            {
                Country = new SelectList(country, "ID", "Name")
            };
            return View(model);
        }      

        [HttpPost]
        public ActionResult Add(CityViewModel model)
        {
            if (ModelState.IsValid)
            {
                City city = new City
                {
                    CountryID = model.CountryID,                    
                    Name = model.Name.Trim(),
                    Description = model.Description
                };
                CityBOL.Insert(city);
            }
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Edit(int cityID)
        {
            var city = CityBOL.GetCityById(cityID);
            var country = CountryBOL.GetAllCountry();
            var model = new CityViewModel
            {
                CreateAt = city.CreateAt,
                CreateBy = city.CreateBy,
                Description = city.Description,
                ID = city.ID,
                Name = city.Name,
                CountryID = city.CountryID,                
                Country = new SelectList(country, "ID", "Name")
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(CityViewModel model)
        {
            if (ModelState.IsValid)
            {
                City city = new City
                {
                    CountryID = model.CountryID,
                    Name = model.Name.Trim(),
                    Description = model.Description,
                    CreateAt = model.CreateAt,
                    CreateBy = model.CreateBy,
                    ID = model.ID
                };
                CityBOL.Update(city);
            }
            //return View(model);
            return RedirectToAction("Index");
        }


        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _DeleteAjaxEditing(int id)
        {
            //Find a customer with ProductID equal to the id action parameter            
            if (id > 0)
            {
                //Delete the record
                if (!CityBOL.Delete(id))
                {
                    ModelState.AddModelError(String.Empty, @"This City have office or media. Please check again!");
                }
            }
            //Rebind the grid
            return View(new GridModel(CityBOL.GetAllCityJoin()));
        }
    }
}
